package com.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 缓存操作工具类
 */
@Component
public class RedisUtil {
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 设置Redis缓存
     * @param key 缓存键值
     * @param value 缓存值
     * @param expireTime 过期时间
     * @param timeUnit 过期时间单位
     */
    public void setCache(String key, String value, Long expireTime, TimeUnit timeUnit) {
        redisTemplate.opsForValue().set(key, value, expireTime, timeUnit);
    }

    /**
     * 设置Redis缓存
     * @param key 缓存键值
     * @param value 缓存值
     */
    public void setCache(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 根据键值删除缓存
     * @param key 缓存键值
     */
    public void deleteCache(String key) {
        redisTemplate.delete(key);
    }
}
